/*
 * Copyright © 2021 http://www.hn-szzy.com/ All rights reserved.
 */
package com.xjh.basestudy.javabase.thread.synctype;

/**
 * @author： 徐建华
 * @date： 2022/1/16
 * @description：
 */

public class TestSyncBlock implements Runnable {
    static TestSyncBlock instance = new TestSyncBlock();
    static int i = 0;

    @Override
    public void run() {
        // 调用
        try {
            method();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //这里用了sync修饰方法
    public synchronized void method() throws InterruptedException {
        for (int j = 0; j < 100000; j++) {
            i++;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(i);
    }
}
